package aad.storyboard.controller;

import aad.storyboard.R;
import aad.storyboard.view.ControlView;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

public class NavigationController implements SurfaceHolder.Callback{
	
	
	private boolean isPaused=false;
	private long lastActionTime=0L;	
	private ControlView surface;
	private SurfaceHolder holder;	
	private View controlPanel=null;
	
	private ImageButton pauseBtn=null;
	private ImageButton forwardBtn=null;
	private ImageButton backwardBtn=null;
	
	
	boolean pausing = false;
	SeekBar volControl=null;

	AudioManager audioManager;
	int maxVolume;
	int curVolume;
	
	public NavigationController(SeekBar vol, Activity a)
	{
		this.volControl=vol;
		a.getWindow().setFormat(PixelFormat.UNKNOWN);
		
		audioManager = (AudioManager)a.getSystemService(Context.AUDIO_SERVICE);
	    maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    volControl.setMax(maxVolume);
    	volControl.setProgress(curVolume);
    	
		
		surface=(ControlView)a.findViewById(R.id.surface);
		surface.addTapListener(onTap);
		holder=surface.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		controlPanel=a.findViewById(R.id.navigation_panel);		
		pauseBtn=(ImageButton)a.findViewById(R.id.pause);
		forwardBtn=(ImageButton)a.findViewById(R.id.forward);
		backwardBtn=(ImageButton)a.findViewById(R.id.backward);    
		 	
    	volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

 @Override
 public void onStopTrackingTouch(SeekBar arg0) {
	 lastActionTime=SystemClock.elapsedRealtime();;
	 System.out.println("Stop"+lastActionTime);
 	

 }

 @Override
 public void onStartTrackingTouch(SeekBar arg0) {
	 lastActionTime=SystemClock.elapsedRealtime();;
	 System.out.println("Start"+lastActionTime); 	

 }

 @Override
 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
  // TODO Auto-generated method stub
  audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
 }
});
    	surface.postDelayed(onEverySecond, 1000);

	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private ControlView.TouchListener onTap=
		new ControlView.TouchListener() {
		@Override
		public void onTap(MotionEvent event) {	

			if (event.getY()<surface.getHeight()/2) {
				
				System.out.println("OnTap topPanel visble");
			}
			else {
				controlPanel.setVisibility(View.VISIBLE);				
				lastActionTime=SystemClock.elapsedRealtime();
			}
		}
	};

	private void clearControl(boolean both) {
		lastActionTime=0;	
		controlPanel.setVisibility(View.GONE);	
	}


	private Runnable onEverySecond=new Runnable() {
		@Override
		public void run() {
			if (lastActionTime>0 &&
					SystemClock.elapsedRealtime()-lastActionTime>3000) {			
				clearControl(false);
			}
			if (!isPaused) {
				surface.postDelayed(onEverySecond, 1000);				
			}
		}
	};

}
